﻿namespace WMS6.BLL
{
    using System;
    using System.Collections.Generic;
    using System.Data;
    using System.Data.Common;
    using WMS6.Common;
    using WMS6.DataEntity;
    using WMS6.DBUtility;
    using WMS6.IDAL;
    using WMS6.QueryEntity;
    using WMS6.DALFactory;

    public class ItemNoBLL : IBaseInfo
    {
        private static readonly ICommonMethodDA commonMethod = DataAccess.CreateCommonMethod();
        private List<string> dataBaseID;
        private static readonly IItemNoDA itemNoDA = DataAccess.CreateItemNoDA();
        private PubParasInfo pubParasInfo;
        private static readonly ISkuDA skuDA = DataAccess.CreateSkuDA();

        public ItemNoBLL()
        {
            this.pubParasInfo = null;
        }

        public ItemNoBLL(string currentWH)
        {
            this.pubParasInfo = null;
            if (!string.IsNullOrEmpty(currentWH))
            {
                this.dataBaseID = commonMethod.GetDataBaseID(currentWH);
                if (this.dataBaseID == null)
                {
                    PublicMethod.GetInstance().ShowAlertMessage("C001");
                }
            }
            else
            {
                PublicMethod.GetInstance().ShowAlertMessage("C002");
            }
        }

        public ItemNoBLL(PubParasInfo tempPubParasInfo)
        {
            this.pubParasInfo = null;
            if (!string.IsNullOrEmpty(tempPubParasInfo.CurrentWH))
            {
                this.dataBaseID = commonMethod.GetDataBaseID(tempPubParasInfo.CurrentWH);
                if (this.dataBaseID == null)
                {
                    PublicMethod.GetInstance().ShowAlertMessage("C001", this.pubParasInfo.UserLanguage);
                }
                else
                {
                    this.pubParasInfo = new PubParasInfo();
                    this.pubParasInfo = tempPubParasInfo;
                }
            }
            else
            {
                PublicMethod.GetInstance().ShowAlertMessage("C002", this.pubParasInfo.UserLanguage);
            }
        }

        public string Delete(List<string> IDS)
        {
            DataBase dataBase = new DataBase();
            DbConnection conn = dataBase.connection;
            conn.Open();
            DbTransaction tran = conn.BeginTransaction();
            string temp = string.Empty;
            try
            {
                int i;
                List<ItemNoInfo> ItemNoInfos = new List<ItemNoInfo>();
                ItemNoInfo itemNoInfo = null;
                foreach (string id in IDS)
                {
                    itemNoInfo = itemNoDA.GetItemNoByID(dataBase, tran, id, this.dataBaseID[0]);
                    ItemNoInfos.Add(itemNoInfo);
                }
                AuditDataBLL auditDataBLL = new AuditDataBLL();
                string count = "";
                foreach (string id in IDS)
                {
                    DataSet SkuDS = skuDA.CheckSkuIDByItemNoID(dataBase, tran, id, this.dataBaseID[0]);
                    i = 0;
                    while (i < SkuDS.Tables[0].Rows.Count)
                    {
                        count = count + SkuDS.Tables[0].Rows[i][0] + ",";
                        i++;
                    }
                    if (!string.IsNullOrEmpty(count))
                    {
                        count = count.Substring(0, count.Length - 1);
                        throw new Exception("项号:" + id + "在货品" + count + "中有引用,无法删除!请先删除货品再删除此项号!");
                    }
                    SkuDS.Clear();
                }
                if (itemNoDA.DeleteItemNo(dataBase, tran, IDS, this.dataBaseID[0]) > 0)
                {
                    for (i = 0; i < IDS.Count; i++)
                    {
                        auditDataBLL.InsertAuditData<ItemNoInfo>(dataBase, tran, ItemNoInfos[i], null, AuditAction.Delete, this.pubParasInfo.UserID, "ITEM_NO", this.dataBaseID[0]);
                        temp = temp + IDS[i] + ",";
                    }
                    temp = temp.Substring(0, temp.Length - 1);
                }
                tran.Commit();
            }
            catch (Exception ex)
            {
                tran.Rollback();
                CommonErrorHandler.ErrorHandler(ex, LogType.Error);
            }
            finally
            {
                conn.Close();
            }
            return temp;
        }

        public DataSet GetAllInfo()
        {
            return itemNoDA.GetAllItemNo(this.dataBaseID[0]);
        }

        public object GetInfoByID(string id)
        {
            return itemNoDA.GetItemNoByID(id, this.dataBaseID[0]);
        }

        public DataSet GetInfoByQueryEntity(object obj)
        {
            ItemNoQueryEntity itemNoQueryEntity = obj as ItemNoQueryEntity;
            List<string> sqlWhere = new List<string>();
            if (!string.IsNullOrEmpty(itemNoQueryEntity.ItemNoID))
            {
                sqlWhere.Add(" ITEM_NO_ID " + itemNoQueryEntity.ItemNoID);
            }
            if (!string.IsNullOrEmpty(itemNoQueryEntity.Descr))
            {
                sqlWhere.Add(" DESCR " + itemNoQueryEntity.Descr);
            }
            if (!string.IsNullOrEmpty(itemNoQueryEntity.IsActive))
            {
                sqlWhere.Add(" IS_ACTIVE ='" + itemNoQueryEntity.IsActive + "'");
            }
            return itemNoDA.GetItemNoByQueryList(sqlWhere, itemNoQueryEntity, this.dataBaseID[0]);
        }

        public string Insert(object obj)
        {
            ItemNoInfo itemNoInfo = obj as ItemNoInfo;
            string whLoginID = this.dataBaseID[0];
            DataBase dataBase = new DataBase();
            DbConnection conn = dataBase.connection;
            conn.Open();
            DbTransaction tran = conn.BeginTransaction();
            string strItemNoID = string.Empty;
            try
            {
                if (itemNoDA.CheckItemNoIDUniqueInsert(dataBase, tran, itemNoInfo.ItemNoID, whLoginID))
                {
                    throw new Exception("项号:" + itemNoInfo.ItemNoID + "已存在,请重新输入!");
                }
                if (string.IsNullOrEmpty(itemNoInfo.ItemNoID))
                {
                    itemNoInfo.ItemNoID = commonMethod.GenNextNumStr(dataBase, tran, "ITEM_NO", whLoginID);
                }
                itemNoInfo.UpdatedBy = itemNoInfo.CreatedBy;
                itemNoInfo.CreatedDate = DateTime.Now;
                itemNoInfo.UpdatedDate = DateTime.Now;
                itemNoInfo.WhID = whLoginID;
                if (itemNoDA.InsertItemNo(dataBase, tran, itemNoInfo, whLoginID) > 0)
                {
                    strItemNoID = itemNoInfo.ItemNoID;
                    new AuditDataBLL().InsertAuditData<ItemNoInfo>(dataBase, tran, null, itemNoInfo, AuditAction.Insert, this.pubParasInfo.UserID, "ITEM_NO", this.dataBaseID[0]);
                }
                tran.Commit();
            }
            catch (Exception ex)
            {
                tran.Rollback();
                CommonErrorHandler.ErrorHandler(ex, LogType.Error);
            }
            finally
            {
                conn.Close();
            }
            return strItemNoID;
        }

        public string Update(object obj)
        {
            ItemNoInfo itemNoInfo = obj as ItemNoInfo;
            string whLoginID = this.dataBaseID[0];
            DataBase dataBase = new DataBase();
            DbConnection conn = dataBase.connection;
            conn.Open();
            DbTransaction tran = conn.BeginTransaction();
            string strItemNoID = string.Empty;
            try
            {
                ItemNoInfo olditemNoInfo = itemNoDA.GetItemNoByID(dataBase, tran, itemNoInfo.ItemNoID, this.dataBaseID[0]);
                if (itemNoDA.UpdateItemNo(dataBase, tran, itemNoInfo, whLoginID) > 0)
                {
                    strItemNoID = itemNoInfo.ItemNoID;
                    new AuditDataBLL().InsertAuditData<ItemNoInfo>(dataBase, tran, olditemNoInfo, itemNoInfo, AuditAction.Update, this.pubParasInfo.UserID, "ITEM_NO", this.dataBaseID[0]);
                }
                tran.Commit();
            }
            catch (Exception ex)
            {
                tran.Rollback();
                CommonErrorHandler.ErrorHandler(ex, LogType.Error);
            }
            finally
            {
                conn.Close();
            }
            return strItemNoID;
        }
    }
}

